﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Visifire.Charts;
using WpfChart.DataStruct;
using System.Collections.ObjectModel;

namespace WpfChart
{
    /// <summary>
    /// LineChartDemo.xaml 的交互逻辑
    /// </summary>
    public partial class LineChartDemo : Window
    {
        ObservableCollection<StockData> stockObjColle = new ObservableCollection<StockData>();

        public LineChartDemo()
        {
            InitializeComponent();
            QuickLineBind();
        }

        #region 分时图

        //List<KeyValuePair<Double, Double>> values;
        private Random _rand = new Random();
        int MaxPoints = 300;

        public void QuickLineBind()
        {
            GetStockData();
            AddDataPoints();

            //多图拖动
            MyChart.AxesX[0].OnZoom += new EventHandler<AxisZoomEventArgs>(MyChart_OnZoom);
            MyChart2.AxesX[0].OnZoom += new EventHandler<AxisZoomEventArgs>(MyChart2_OnZoom);
            MyChart.AxesX[0].Scroll += new EventHandler<AxisScrollEventArgs>(MyChart_Scroll);
            MyChart2.AxesX[0].Scroll += new EventHandler<AxisScrollEventArgs>(MyChart2_Scroll);

            //定时器
            System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherLineTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            dispatcherTimer.Start();
        }

        /// <summary>
        /// 动态更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dispatcherLineTimer_Tick(object sender, EventArgs e)
        {
            DataSeries ds = MyChart.Series[0];
            MyChart.Dispatcher.BeginInvoke(new Action<DataSeries>(UpdateMinute), new object[] { ds });
        }

        /// <summary>
        /// 动态更新datapoint值
        /// </summary>
        /// <param name="ds"></param>
        private void UpdateMinute(DataSeries ds)
        {
            if (ds != null)
            {
                Random rand = new Random();
                DataPoint dp = ds.DataPoints[ds.DataPoints.Count - 1];
                dp.YValue = ds.DataPoints[ds.DataPoints.Count - 2].YValue + rand.Next(-10, 10);
            }
        }

        /// <summary>
        /// 添加DataPoint
        /// </summary>
        private void AddDataPoints()
        {
            //添加分时图
            //x轴刻度
            Axis axisX = new Axis();
            axisX.IntervalType = IntervalTypes.Minutes;
            axisX.ValueFormatString = "HH:mm";
            axisX.Interval = 20;
            axisX.ScrollBarSize = 0;
            MyChart.AxesX.Add(axisX);

            //y轴刻度
            Axis axisY = new Axis();
            axisY.AxisMaximum = 2490;
            axisY.AxisMinimum = 2400;
            MyChart.AxesY.Add(axisY);
            MyChart.Series[0].XValueType = ChartValueTypes.Time;
            MyChart.Series[0].DataSource = stockObjColle;
           
            //添加成交量柱子
            Axis axisX2 = new Axis();
            axisX2.IntervalType = IntervalTypes.Minutes;
            axisX2.ValueFormatString = "HH:mm";
            axisX2.Interval = 20;
            axisX2.ScrollBarSize = 26;
            
            Axis axisY2 = new Axis();
            axisY2.AxisMaximum = 2490;
            axisY2.AxisMinimum = 2410;
            axisY2.AxisType = AxisTypes.Primary;
            //axisY.Prefix = "$";
            MyChart2.AxesX.Add(axisX2);
            MyChart2.AxesY.Add(axisY2);
            MyChart2.Series[0].XValueType = ChartValueTypes.Time;
           
            MyChart2.Series[0].DataSource = stockObjColle;

            //添加持仓线
            Axis axisY3 = new Axis();
            axisY3.AxisType = AxisTypes.Secondary;
            MyChart2.AxesY.Add(axisY3);
            MyChart2.Series[1].AxisYType = AxisTypes.Secondary;
            MyChart2.Series[1].XValueType = ChartValueTypes.Time;
            MyChart2.Series[1].DataSource = stockObjColle;

        }

        //public void GetStockData()
        //{
        //    List<StockData> lstData = new List<StockData>();
        //    StockData data1 = new StockData(2454, 2465, 2470, 2430, "2012-01-01", 100);
        //    StockData data2 = new StockData(2465, 2462, 2465, 2435, "2012-02-01", 99);
        //    StockData data3 = new StockData(2462, 2451, 2460, 2432, "2012-03-01", 89);
        //    StockData data4 = new StockData(2451, 2461, 2472, 2432, "2012-04-01", 98);
        //    StockData data5 = new StockData(2461, 2465, 2473, 2433, "2012-05-01", 67);
        //    StockData data6 = new StockData(2465, 2463, 2474, 2434, "2012-06-01", 78);
        //    StockData data7 = new StockData(2463, 2465, 2475, 2435, "2012-07-01", 87);
        //    StockData data8 = new StockData(2455, 2467, 2476, 2436, "2012-08-01", 67);
        //    StockData data9 = new StockData(2467, 2438, 2463, 2437, "2012-09-01", 66);
        //    StockData data10 = new StockData(2438, 2454, 2456, 2438, "2012-10-01", 65);
        //    StockData data11 = new StockData(2454, 2466, 2457, 2439, "2012-11-01", 71);
        //    StockData data12 = new StockData(2466, 2445, 2447, 2423, "2012-12-01", 72);
        //    StockData data13 = new StockData(2454, 2465, 2470, 2430, "2013-01-01", 76);
        //    StockData data14 = new StockData(2465, 2462, 2465, 2435, "2013-02-01", 73);
        //    StockData data15 = new StockData(2462, 2451, 2460, 2432, "2013-03-01", 52);
        //    StockData data16 = new StockData(2451, 2461, 2472, 2432, "2013-04-01", 55);
        //    StockData data17 = new StockData(2461, 2465, 2473, 2433, "2013-05-01", 58);
        //    StockData data18 = new StockData(2465, 2463, 2474, 2434, "2013-06-01", 43);
        //    StockData data19 = new StockData(2463, 2465, 2475, 2435, "2013-07-01", 37);
        //    StockData data20 = new StockData(2455, 2467, 2476, 2436, "2013-08-01", 30);
        //    StockData data21 = new StockData(2467, 2438, 2463, 2437, "2013-09-01", 66);
        //    StockData data22 = new StockData(2438, 2454, 2456, 2438, "2013-10-01", 68);
        //    StockData data23 = new StockData(2454, 2466, 2457, 2439, "2013-11-01", 61);
        //    StockData data24 = new StockData(2466, 2445, 2447, 2423, "2013-12-01", 22);
        //    StockData data25 = new StockData(2454, 2465, 2470, 2430, "2011-01-01", 26);
        //    StockData data26 = new StockData(2465, 2462, 2465, 2435, "2011-02-01", 29);
        //    StockData data27 = new StockData(2462, 2451, 2460, 2432, "2011-03-01", 37);
        //    StockData data28 = new StockData(2451, 2461, 2472, 2432, "2011-04-01", 31);
        //    StockData data29 = new StockData(2461, 2465, 2473, 2433, "2011-05-01", 47);
        //    StockData data30 = new StockData(2465, 2463, 2474, 2434, "2011-06-01", 49);
        //    StockData data31 = new StockData(2463, 2465, 2475, 2435, "2011-07-01", 43);
        //    StockData data32 = new StockData(2455, 2467, 2476, 2436, "2011-08-01", 77);
        //    StockData data33 = new StockData(2467, 2438, 2463, 2437, "2011-09-01", 75);
        //    StockData data34 = new StockData(2438, 2454, 2456, 2438, "2011-10-01", 90);
        //    StockData data35 = new StockData(2454, 2466, 2457, 2439, "2011-11-01", 74);
        //    StockData data36 = new StockData(2466, 2466, 2467, 2423, "2011-12-01", 80);

        //    lstData.AddRange(new StockData[] { data25,data26,data27,data28,data29,data30,
        //                                       data31,data32,data33,data34,data35,data36,
        //                                       data1, data2, data3, data4, data5, data6, data7, data8, data9, data10,
        //                                       data11, data12,data13,data14,data15,data16,data17,data18,data19,data20,
        //                                       data21,data22,data23,data24});
        //    foreach (StockData data in lstData)
        //        stockObjColle.Add(data);
        //}

        public void GetStockData()
        {
            stockObjColle.Clear();
            Random r = new Random();
            for (int i = MaxPoints; i > 0; i--)
            {
                StockData data = new StockData(r.Next(2420, 2480), r.Next(2420, 2480),
                                  r.Next(2420, 2480), r.Next(2420, 2480), DateTime.Now.AddMinutes(-1 * i).ToString("HH:mm"), r.Next(1, 100));
                stockObjColle.Add(data);
            }
            if (stockObjColle != null && stockObjColle.Count > 0)
            {
                stockObjColle[0].ShowTime = " ";
            }
            //StockData data1 = new StockData(2454, 2465, 2470, 2430, "2012-01-01");
            //StockData data2 = new StockData(2465, 2462, 2465, 2435, "2012-02-01");
            //StockData data3 = new StockData(2462, 2451, 2460, 2432, "2012-03-01");
            //StockData data4 = new StockData(2451, 2461, 2472, 2432, "2012-04-01");
            //StockData data5 = new StockData(2461, 2465, 2473, 2433, "2012-05-01");
            //StockData data6 = new StockData(2465, 2463, 2474, 2434, "2012-06-01");
            //StockData data7 = new StockData(2463, 2465, 2475, 2435, "2012-07-01");
            //StockData data8 = new StockData(2455, 2467, 2476, 2436, "2012-08-01");
            //StockData data9 = new StockData(2467, 2438, 2463, 2437, "2012-09-01");
            //StockData data10 = new StockData(2438, 2454, 2456, 2438, "2012-10-01");
            //StockData data11 = new StockData(2454, 2466, 2457, 2439, "2012-11-01");
            //StockData data12 = new StockData(2466, 2445, 2447, 2423, "2012-12-01");
            //StockData data13 = new StockData(2454, 2465, 2470, 2430, "2013-01-01");
            //StockData data14 = new StockData(2465, 2462, 2465, 2435, "2013-02-01");
            //StockData data15 = new StockData(2462, 2451, 2460, 2432, "2013-03-01");
            //StockData data16 = new StockData(2451, 2461, 2472, 2432, "2013-04-01");
            //StockData data17 = new StockData(2461, 2465, 2473, 2433, "2013-05-01");
            //StockData data18 = new StockData(2465, 2463, 2474, 2434, "2013-06-01");
            //StockData data19 = new StockData(2463, 2465, 2475, 2435, "2013-07-01");
            //StockData data20 = new StockData(2455, 2467, 2476, 2436, "2013-08-01");
            //StockData data21 = new StockData(2467, 2438, 2463, 2437, "2013-09-01");
            //StockData data22 = new StockData(2438, 2454, 2456, 2438, "2013-10-01");
            //StockData data23 = new StockData(2454, 2466, 2457, 2439, "2013-11-01");
            //StockData data24 = new StockData(2466, 2445, 2447, 2423, "2013-12-01");
            //StockData data25 = new StockData(2454, 2465, 2470, 2430, "2011-01-01");
            //StockData data26 = new StockData(2465, 2462, 2465, 2435, "2011-02-01");
            //StockData data27 = new StockData(2462, 2451, 2460, 2432, "2011-03-01");
            //StockData data28 = new StockData(2451, 2461, 2472, 2432, "2011-04-01");
            //StockData data29 = new StockData(2461, 2465, 2473, 2433, "2011-05-01");
            //StockData data30 = new StockData(2465, 2463, 2474, 2434, "2011-06-01");
            //StockData data31 = new StockData(2463, 2465, 2475, 2435, "2011-07-01");
            //StockData data32 = new StockData(2455, 2467, 2476, 2436, "2011-08-01");
            //StockData data33 = new StockData(2467, 2438, 2463, 2437, "2011-09-01");
            //StockData data34 = new StockData(2438, 2454, 2456, 2438, "2011-10-01");
            //StockData data35 = new StockData(2454, 2466, 2457, 2439, "2011-11-01");
            //StockData data36 = new StockData(2466, 2445, 2447, 2423, "2011-12-01");

            //lstData.AddRange(new StockData[] { data25,data26,data27,data28,data29,data30,
            //                                   data31,data32,data33,data34,data35,data36,
            //                                   data1, data2, data3, data4, data5, data6, data7, data8, data9, data10,
            //                                   data11, data12,data13,data14,data15,data16,data17,data18,data19,data20,
            //                                   data21,data22,data23,data24});
            //foreach (StockData data in lstData)
            //    stockObjColle.Add(data);

            //return lstData;
        }



        /// <summary>
        /// 创建DataPoint
        /// </summary>
        /// <returns></returns>
        //private DataPoint CreateDataPoint()
        //{
        //    KeyValuePair<Double, Double> prevValue;

        //    if (values.Count > 0)
        //        prevValue = values[values.Count - 1];
        //    else
        //        prevValue = new KeyValuePair<Double, double>();

        //    Double xValue = values.Count > 0 ? prevValue.Key + 1 : values.Count;

        //    DataPoint dataPoint = new DataPoint();

        //    //dataPoint.XValue = xValue;

        //    Double value;

        //    if (values.Count > 0)
        //        value = prevValue.Value;
        //    else
        //        value = 10;

        //    if (_rand.NextDouble() > 0.5)
        //        value = value + _rand.NextDouble();
        //    else
        //        value = value - _rand.NextDouble();

        //    dataPoint.YValue = value;

        //    values.Add(new KeyValuePair<Double, double>(xValue, value));

        //    return dataPoint;
        //}


        #endregion

        #region 多图拖动关联

        void MyChart_Scroll(object sender, AxisScrollEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart2.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart2_Scroll(object sender, AxisScrollEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart_OnZoom(object sender, AxisZoomEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart2.AxesX[0].Zoom(e.MinValue, e.MaxValue);
            MyChart2.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        void MyChart2_OnZoom(object sender, AxisZoomEventArgs e)
        {
            Axis axis = sender as Axis;
            MyChart.AxesX[0].Zoom(e.MinValue, e.MaxValue);
            MyChart.AxesX[0].ScrollBarOffset = axis.ScrollBarOffset;
        }

        #endregion


        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtNumber.Text))
            {
                stockObjColle.Clear();
                this.MaxPoints = Convert.ToInt32(txtNumber.Text);
                QuickLineBind();
            }
        }


    }
}
